import Mock from 'mockjs'

const list = []

const firseData = Mock.mock({
  id: "@id",
  name: "靓仔",
  sex: 'male',
  age: 22,
  address: "@county(true)",
  birthday: "2003-03-17",
})

list.push(firseData)


const user = {
  listData: (config) => {
    return {
      status: 200,
      message: 'Success',
      data: list,
    }
  },
  addData: (config) => {
    const data = JSON.parse(config.body)
    const { id } = Mock.mock({
      id: "@id",
    })
    list.push({
      id,
      ...data,
    })
    return {
      status: 200,
      message: 'Success',
      data: list,
    }
  },
  editData: (config)=>{
    const data = JSON.parse(config.body)
    const index = list.findIndex(item => item.id === data.id)
    if(index!== -1){
      list[index] = {...data}
    }
    return {
      status: 200,
      message: 'Success',
      data: list,
    }
  },
  deleteData: (config) => {
    const data = JSON.parse(config.body)
    const index = list.findIndex(item => item.id === data.id)
    if(index!== -1){
      list.splice(index, 1)
    }
    return {
      status: 200,
      message: 'Success',
      data: list,
    }
  }
}

export default user